package com.lagou.lagouservicecode.controller;

import com.lagou.api.MailApi;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @Authther: mrwu
 * @date:2021/4/14-04
 * @Description:com.lagou.lagouservicecode.controller
 */
@RefreshScope
@RestController
public class CodeController {
    static final String CODE_PRI_FIX ="mailcode:";

     @DubboReference(timeout = 2000) //使用dubbo代理
     MailApi mailApi;

    @Autowired
    RedisTemplate<String,String> redisTemplate;

    //生成验证码
    @GetMapping("/code/{email}") //js调用网关网关调用
    public Boolean sendCode(@PathVariable(name = "email") String email){
        ValueOperations opsForValue = redisTemplate.opsForValue();//字符串操作连接
        //先从缓存中取出数据//如果有，直接返回，如果没有调用下游服务
        Object o = opsForValue.get(CODE_PRI_FIX + email);
        if (o ==null){
          //生成验证验证码
        }
        boolean result = mailApi.sendMail(email, "123456");
        if (result){
            //将验证码存入redis
            System.out.println( "redis设置值========="+CODE_PRI_FIX+email);
            opsForValue.set(CODE_PRI_FIX+email, "123456", 10, TimeUnit.MINUTES);//设置10分钟超时
        }
        return result;
    }
}
